package com.visionork.android.twe.activity;

import java.util.ArrayList;
import java.util.List;

import com.visionork.android.twe.RepositoryKey;
import com.visionork.android.twe.TWEApplication;
import com.visionork.android.twe.model.Episode;
import com.visionork.android.twe.model.RepositoryBase;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.res.Resources;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;


public class ProgramActivity extends ListActivity {
	public static final String KEY = "KEY";
	public static final String REPOSITORY = "REPOSITORY";
	private String key;
	private int repositoryKey;
	
	private class LoadEpisodeTask extends AsyncTask<Void, Void, Void> {
		protected ProgressDialog _pd;
		private ArrayList<Episode> episodes;

		public LoadEpisodeTask() {
			episodes = new ArrayList<Episode>();
		}

		@Override
		protected Void doInBackground(Void... params) {
			try {
				episodes = getRepository().getProgram(key)
						.getEpisodes();
			} catch (Throwable e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onPreExecute() {
			Resources res = getResources();
			_pd = ProgressDialog.show(ProgramActivity.this,
					res.getString(R.string.d_loading),
					res.getString(R.string.d_loading_detail), true, false);
		}

		@Override
		protected void onPostExecute(Void param) {
			List<String> titles = new ArrayList<String>(episodes.size());

			for (Episode episode : episodes) {
				titles.add(episode.getTitle());
			}

			setListAdapter(new ArrayAdapter<String>(ProgramActivity.this,
					R.layout.episode_list_item, titles));
			
			_pd.dismiss();
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.episode_list);
		key = getIntent().getStringExtra(KEY);
		repositoryKey = getIntent().getIntExtra(REPOSITORY, RepositoryKey.ShowRepository);
		loadEpisode();
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		Episode episode = getRepository().getProgram(key)
				.getEpisodes().get(position);
		Intent intent = new Intent(this, EpisodeActivity.class);
		intent.putExtra(EpisodeActivity.PROGRAMKEY, key);
		intent.putExtra(EpisodeActivity.KEY, episode.getKey());
		intent.putExtra(EpisodeActivity.REPOSITORY, repositoryKey);
		startActivity(intent);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.program_list_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.refresh:
			getRepository().getProgram(key).refresh();
			loadEpisode();
			getRepository().persistConfig();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	private void loadEpisode() {
		LoadEpisodeTask task = new LoadEpisodeTask();
		task.execute(new Void[] {});
	}

	protected RepositoryBase getRepository() {
		return TWEApplication.getRepository(repositoryKey);
	}
}
